<template>
  <div class="model">
    <div class="mask" @click="changeIsShowLoginModel(false)"></div>
    <div class="login-box">
      <!-- 导航栏 -->
      <section class="nav">
        <!-- 关闭按钮 -->
        <div class="close" @click="changeIsShowLoginModel(false)"></div>
        <a :class="{ active: isShowPhone }" href="#" @click="isShowPhone = true"
          >手机号登录</a
        >
        |
        <a
          :class="{ active: !isShowPhone }"
          href="#"
          @click="isShowPhone = false"
          >微信扫码登录</a
        >
      </section>
      <div class="content">
        <div class="phone-login" v-show="isShowPhone">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model="phoneNumber"
              class="ipt"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="arr"
              :accuracy="200"
            ></slide-verify>
          </div>
          <div class="row">
            <input
              type="text"
              placeholder="请输入短信验证码"
              class="ipt"
              v-model="code"
            />
            <!-- <button class="getCode-btn" @click="getCodeFn">
              获取验证码{{ count }}
            </button> -->
            <button
              class="getCode-btn"
              @click="getCodeFn"
              v-show="!isShowCount"
            >
              获取验证码
            </button>
            <button class="getCode-btn" v-show="isShowCount">
              {{ count + "s" }}
            </button>
          </div>
          <div class="row">
            <button class="login-btn" @click="submitFn">登录</button>
          </div>
        </div>
        <div class="pr-login" v-show="!isShowPhone">微信扫码登录内容</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions } from "vuex";
import { sendSMSAPI, phoneReginAPI } from "@/request/api";
import { validatePhoneNumber } from "@/utils/index";
export default {
  data() {
    return {
      isShowPhone: true,
      msg: "向右滑动",
      arr: [
        require("../assets/img/integral-01.9386d4bf.png"),
        require("../assets/img/integral-02.150d92a1.png"),
      ],
      phoneNumber: "",
      count: 59,
      isShowCount: false,
      code: "",
    };
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModel: "showModel/changeIsShowLoginModel",
      changeIsLoginStatus: "loginStatus/changeIsLoginStatus",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "拼图成功, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 倒计时
    countDown() {
      this.isShowCount = true;
      let timer = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          this.count = 59;
          clearInterval(timer);
          this.isShowCount = false;
        }
      }, 1000);
    },
    // 校验手机号和滑块验证码
    toVerify() {
      // 手机号格式
      // let reg =
      //   /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (!validatePhoneNumber(this.phoneNumber)) {
        // return alert("请输入正确的手机号");
        /* 
        this.changeIsShowToast({
          isShow: true,
          msg: "请输入正确的手机号",
          type: "warning",
        });
        setTimeout(() => {
          this.changeIsShowToast({
            isShow: false,
          });
        }, 1000); 
        */
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请输入正确的手机号",
          type: "warning",
        });
        return;
      }
      // 滑块验证码校验
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        // return alert("请先完成滑块验证码");
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请先完成滑块验证码",
          type: "danger",
        });
        return;
      }
      return true;
    },
    async getCodeFn() {
      // console.log(this.toVerify());
      if (!this.toVerify()) return;
      // console.log(123);
      // 验证码倒计时
      let res = await sendSMSAPI({
        phone: this.phoneNumber,
      });
      if (!res) return;
      // alert("发送成功");
      this.asyncChangeIsShowToast({
        isShow: true,
        msg: "发送成功",
        type: "success",
      });
      this.countDown();
    },
    // 滑块验证码校验
    async submitFn() {
      // 验证手机号滑块验证码
      if (!this.toVerify()) return;
      // 验证码非空校验
      if (this.code.trim() == "") {
        // alert("请输入验证码！");
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请输入验证码！",
          type: "warning",
        });
        this.code = "";
        return;
      }
      // 发送登录请求
      let res = await phoneReginAPI({
        verifyCode: this.code,
        phone: this.phoneNumber,
      });
      if (!res) return;
      localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
      this.changeIsShowLoginModel(false);
      this.changeIsLoginStatus(true);
      // alert("登录成功");
      this.asyncChangeIsShowToast({
        isShow: true,
        msg: "登录成功",
        type: "success",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.model {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .login-box {
    position: absolute;
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    inset: 0;
    margin: auto;
    .nav {
      text-align: center;
      margin-top: 20px;
      .close {
        position: absolute;
        right: 60px;
        top: 18px;
        cursor: pointer;
        width: 22px;
        height: 22px;
        background: url("../assets/img/close.png");
      }
      a {
        text-decoration: none;
        color: gray;
        font-size: 18px;
        &.active {
          color: #f40;
        }
      }
    }
    .content {
      width: 355px;
      height: 300px;
      margin: 30px auto 0;
      .phone-login {
        .row {
          display: flex;
          margin-bottom: 30px;
          button {
            border: none;
            background-color: #0a328e;
            cursor: pointer;
            color: #fff;
            border-radius: 5px;
            font-size: 16px;
          }
          .ipt {
            height: 50px;
            flex: 1;
            padding-left: 20px;
            outline: none;
          }
          .getCode-btn {
            margin-left: 10px;
            width: 95px;
          }
          .login-btn {
            width: 100%;
            height: 50px;
          }
        }
      }
    }
  }
}
/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>